import { getUserDetail } from '@/http/my'


export const updateUserLoginInfo = async (successBack?: Function, fail?: Function) :Promise<any> => { // 更新用户信息
	let user = await getUserDetail()
	if(user?.code === 0) {
		uni.setStorage({
			key: 'userInfo',
			data: JSON.stringify(user.data || null),
			success () { 
				successBack && successBack()
			}
		})
		
	} else {
		fail && fail(user.msg)
	}
}

export const getOrderTime = (isS = false, time?: number): string => { // 创建订单所需要得时间
	let date: Date;
	if(time) {
		date = new Date(time)
	} else {
		date = new Date()
	}
	let y = date.getFullYear()
	let M = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : `0${date.getMonth() + 1}`
	let d = date.getDate() > 9 ? date.getDate() : `0${date.getDate()}`
	
	let h = date.getHours() > 9 ? date.getHours() : `0${date.getHours()}`
	let m = date.getMinutes() > 9 ? date.getMinutes() : `0${date.getMinutes()}`
	let s = date.getSeconds() > 9 ? date.getSeconds() : `0${date.getSeconds()}`
	
	let str = `${y}-${M}-${d} ${h}:${m}`
	if(isS) {
		str = `${y}-${M}-${d} ${h}:${m}:${s}`
	}
	
	return str
}

export const getLocationInfo = async () :Promise<{data: any, code: 1 | 0, latitude: number, longitude: number}> => { // 获取地理位置信息
	return new Promise((resolve, reject) => {
		uni.getLocation({
			type: 'gcj02',
			geocode: true,
			success(res) {  
				resolve({data: res?.address, code: 1, latitude: res.latitude, longitude: res.longitude})
			},
			fail(err) { 
				reject({data: err, code: -1})
			}
		})
	})
}

export const awaitNextTick = (time:number = 3000, cab?: Function) => { // 延迟函数
	return new Promise((resolve) => {
		let timer = setTimeout(() => {
			cab && cab()
			clearTimeout(timer)
			resolve(1)
		}, time)
		
	})
}